package com.wjx.dingcan.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.BeanUtil;
import com.kzw.core.util.ContextUtil;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.web.action.BaseAction;
import com.kzw.oa.model.system.AppUser;
import com.kzw.oa.util.JSON;
import com.mchange.v2.codegen.bean.BeangenUtils;
import com.wjx.dingcan.model.Card;
import com.wjx.dingcan.model.Food;
import com.wjx.dingcan.service.CardService;
import com.wjx.dingcan.service.FoodService;

@Controller
@Scope("prototype")
public class CardAction extends BaseAction {
	
	@Autowired
	private FoodService foodService;
	@Autowired
	private CardService cardService;

	/**
	 * 显示food
	 */
	public String list() {
		HttpServletRequest request = getRequest();
		
		AppUser user = ContextUtil.getCurrentUser();
		Card card = cardService.findUniqueBy("owner", user);
		if(card == null) {
			return SUCCESS;
		}
		
		jsonString = new JSON(card).buildWithFilters(3);
		return SUCCESS;
	}
	
	/**
	 * 向购物车中添加一个食物 
	 * @return
	 */
	public String add() {
		
		HttpServletRequest request = getRequest();
		Long food_oid = Long.parseLong(request.getParameter("oid"));
		
		AppUser current = ContextUtil.getCurrentUser();

		Card card = cardService.findUniqueBy("owner", current);
		if(card == null) {
			card = new Card();
			card.setOwner(current);
			
		}
		
		Food food = foodService.get(food_oid);
		card.getFoods().add(food);
		cardService.saveOrUpdate(card);
		
		return SUCCESS;
	}
	
	/**
	 * remove foods by oids
	 * @return
	 */
	public String remove() {
		HttpServletRequest request = getRequest();
		Long food_oid = Long.parseLong(request.getParameter("oid"));
		
		Food food = foodService.get(food_oid);
		AppUser current = ContextUtil.getCurrentUser();
		Card card = cardService.findUniqueBy("owner", current);	
		card.getFoods().remove(food);
		cardService.saveOrUpdate(card);
		
		try {
			getResponse().sendRedirect("/dingcan/htmls/cart.html");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	 
	/**
	 * 结算
	 */
	public String count() {
		HttpServletRequest request= getRequest();
		String foodIds = request.getParameter("ids");
		if(foodIds == null || foodIds.equals("") ) {
			return "{\"success\":true,\"total\":\"0\"}";
		}
		
		float prices = 0f;
		float yunfei = 0f;
		
		String[] ids = foodIds.split(",");
		for(String id : ids) {
			Food food = foodService.get(Long.parseLong(id));
			prices += food.getFoodPrice();
			yunfei += food.getYunfei();
		}
		
		try {
			getResponse().sendRedirect("/dingcan/htmls/confirm_order.html?total=" + prices 
					+ "&yunfei=" + yunfei + "&ids=" + foodIds);
		} catch (IOException e) {
			e.printStackTrace();
			
		}
//		return "{\"success\":true,\"total\":" + prices +"}";
		return SUCCESS;
	}
 

}
